trigger tenancyTriggerForRenewalSteps on Tenancy_Contract__c (before update, after update) 
{
    List<Inspection__c> inspectionInsertionList = new List<Inspection__c>();
    Map<ID,ID>  accountIDForOwnerChangeMap = new Map<ID,ID>();
    Set<ID> tenancyContractIds = new Set<ID>();
    
    //For SLA Implementation... (Record Update...)
    if(Trigger.isUpdate && Trigger.isAfter)
    {
        for(Tenancy_Contract__c tenancyObject : Trigger.New)
        {
            if(Trigger.oldMap.get(tenancyObject.id).Tenancy_Contract_Renewal_Steps__c != tenancyObject.Tenancy_Contract_Renewal_Steps__c)
            {
                if(tenancyObject.Tenancy_Contract_Renewal_Steps__c == 'Approved by Delivery Department User')
                {
                    tenancyContractIds.add(tenancyObject.Id);
                    
                }
            }
        
        }
        
        
        if(tenancyContractIds.size() > 0)
        {
            List<SLA__c> slaList = new List<SLA__c>();
        
            for(SLA__c slaObject : [SELECT ID, isCompleted__c, Actual_End_Date_Time__c, Actual_Start_Date_Time__c, Amendment__c, Application_Type__c, Expected_End_Date__c, License__c, Process_Type__c, SLA_Hours__c, Status__c, Tenancy_Contract__c, isCancelled__c  FROM SLA__c where Tenancy_Contract__c IN: tenancyContractIds AND isCompleted__c =: false AND License__c =: null AND Amendment__c =: null AND isCancelled__c = false ORDER BY CreatedDate])
            {
            
                //Putting the Actual_End_Date_Time__c End Date of SLA...
                slaObject.Actual_End_Date_Time__c = DateTime.Now();
                slaObject.isCompleted__c =true;
                //Adding in the List...
                slaList.add(slaObject);
            }
            
            //Upserting the SLA List...
            if(slaList.size() > 0)
                upsert slaList;
        
        }
    
    }
    
    
    if(Trigger.isUpdate && Trigger.isBefore)
    {
        
        for(Tenancy_Contract__c tenancyObject : Trigger.New)
        {
            if(tenancyObject.Record_Type_Name__c == 'Under_Renewal')
            {
                //if(Trigger.isAfter && Trigger.isUpdate)
                //{
                    //For Account Record Owner Changes...
                    if(tenancyObject.Account_Record_Id__c != null)
                    {
                        if(Trigger.oldMap.get(tenancyObject.Id).Account_Record_Id__c != tenancyObject.Account_Record_Id__c)
                        {
                            //Change the Account Owner Change Map...
                            accountIDForOwnerChangeMap.put(tenancyObject.Tenant__c ,tenancyObject.Account_Record_Id__c);
                        }
                        
                    }
                //}
                
                //For Inspection Record...
                if(Trigger.oldMap.get(tenancyObject.id).Tenancy_Contract_Renewal_Steps__c != tenancyObject.Tenancy_Contract_Renewal_Steps__c)
                {
                    if(tenancyObject.Tenancy_Contract_Renewal_Steps__c == 'Submitted To Maintenance Department')
                    {
                        if(tenancyObject.Location_type__c == 'Land' || tenancyObject.Location_type__c == 'Car Show Room' || tenancyObject.Location_type__c == 'Warehouse')
                        {
                            RecordType inspectionRecordType = [Select Id,DeveloperName,SObjectType FROM RecordType where SObjectType =: 'Inspection__c' AND IsActive =: true AND DeveloperName =: 'License_Inspection'];
                            
                            //Creating the Inspection Records...
                            Inspection__c inspectionObject = new Inspection__c();
                            inspectionObject.Company__c = tenancyObject.Tenant__c;
                            inspectionObject.RecordTypeId = inspectionRecordType.Id;
                            inspectionObject.Active__c = 'No';
                            inspectionObject.Location__c = tenancyObject.Location__c;   
                            inspectionObject.Immigration_Approval__c = 'Do Not Agree';
                            inspectionObject.Inspector_Approval__c = 'Do Not Agree';
                            
                            //Adding in the List...
                            inspectionInsertionList.add(inspectionObject);
                            
                            
                        }
                    }
                }   
                
                
            }
            
        }
        
        //For Creating the Inspection Records...
        if(inspectionInsertionList.size() > 0)
            insert inspectionInsertionList;
        
        //For Changing the Account...
        if(accountIDForOwnerChangeMap.size() > 0)
        {
            List<Account> accountList = new List<Account>();
            
            for(Account accountObject : [select id,OwnerId from Account where Id =: accountIDForOwnerChangeMap.keyset()])
            {
                accountObject.Record_Approver_Id__c = accountIDForOwnerChangeMap.get(accountObject.Id);
                accountList.add(accountObject);
            }
            
            
            //Now Update the Account List...
            if(accountList.size() > 0)
                update accountList;
            
            
            
        }
        
        
        
    }

}